package com.example.application.views.login;

import com.example.application.bean.ResultBean;
import com.example.application.bean.User;

import com.example.application.config.URLConfig;
import com.example.application.views.util.MyHttpClientUtil;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

@Route("login")
@RouteAlias("")
public class LoginView extends Div {



    public LoginView(){
        VerticalLayout verticalLayout = new VerticalLayout();
        //创建一个水平布局
        HorizontalLayout layout = new HorizontalLayout();
        //设置距离上面300px
        layout.getStyle().set("width","390px");
        layout.getStyle().set("margin-top","100px");
        layout.getStyle().set("margin-left","auto");
        layout.getStyle().set("margin-right","auto");

        //创建一个登录窗口
        LoginForm component = new LoginForm();
        //开始登录操作
        component.setEnabled(true);
        //关闭忘记密码
        component.setForgotPasswordButtonVisible(false);
        //添加登录事件
        component.addLoginListener(e->{
            String username = e.getUsername();
            String pwd = e.getPassword();

            String url = URLConfig.LOGIN + username + "/" + pwd;

            ObjectMapper mapper = new ObjectMapper();

            ResultBean resultBean =null;
            try (CloseableHttpClient httpClient = HttpClients.createDefault()){

                HttpGet httpGet = new HttpGet(url);
                CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
                MyHttpClientUtil.addSession(httpResponse);


                String json = EntityUtils.toString(httpResponse.getEntity(), "utf-8");

                JavaType javaType = mapper.getTypeFactory().constructParametricType(ResultBean.class, User.class);

                resultBean = mapper.readValue(json, javaType);

            } catch (IOException exception) {
                exception.printStackTrace();
            }

            if("登录成功".equals(resultBean.getMsg())){
                User u = (User) resultBean.getT();
                UI.getCurrent().getSession().setAttribute("user", u);
                String naviget = "";
                if(u.getIdentity().equals("平台管理员")){
                    naviget = "admin";
                }else if("门店管理员".equals(u.getIdentity())){
                    naviget = "store";
                }else if("供应商管理员".equals(u.getIdentity())){
                    naviget = "supplier";
                }
                UI.getCurrent().navigate(naviget);
            }else {
                UI.getCurrent().getPage().executeJs("alert('"+resultBean.getMsg()+"');");
            }
            component.setEnabled(true);
        });

        /*Anchor anchor = new Anchor("enroll","注册");
        anchor.getStyle().set("margin-top","350px");*/

        //注册按钮
        Button enroll = new Button("注册");
        /*enroll.getStyle().set("margin-top","350px");*/
        enroll.getStyle().set("margin-left","20px");
        enroll.getStyle().set("width", "312px");
        enroll.getStyle().set("height", "36px");
        enroll.addClickListener(e->{
            UI.getCurrent().navigate("enroll");

        });
        verticalLayout.add(component, enroll);
        verticalLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);

         //让窗体居中
//        layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
        layout.removeAll();
        //把登录窗体放进水平布局中
        layout.add(verticalLayout);
        this.removeAll();
        //在水平布局添加到容器中
        this.add(layout);
    }

}
